package com.kaichaohulian.baocms.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.adapter.NewHotXuqiuAdapter;
import com.kaichaohulian.baocms.adapter.NewXuQiuAdapter;
import com.kaichaohulian.baocms.http.HttpUtil;
import com.kaichaohulian.baocms.http.Url;
import com.kaichaohulian.baocms.newmodel.NewHotModel;
import com.kaichaohulian.baocms.newmodel.NewXuqiuModel;
import com.kaichaohulian.baocms.util.FullyLinearLayoutManager;
import com.kaichaohulian.baocms.utils.DBLog;
import com.kaichaohulian.baocms.view.ShowDialog;
import com.kaichaohulian.baocms.view.SimpleDividerItemDecoration;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


@SuppressLint("ValidFragment")
public class SimpleCardFragment extends Fragment {

    Unbinder unbinder;
    private String mTitle;
    //    最新需求
    @BindView(R.id.recycle_xuqiu)
    RecyclerView recycle_xuqiu;

    //最热需求
/*    @BindView(R.id.recycle_rexuqiu)
    RecyclerView recycle_rexuqiu;*/
    public static SimpleCardFragment getInstance(String title) {
        SimpleCardFragment sf = new SimpleCardFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_simple_card, null);
    /*    TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);*/
        unbinder = ButterKnife.bind(this, v);

        getnewXuqiu();
//        gethotXuqiu();


        return v;
    }

    public void getnewXuqiu() {

        final RequestParams params = new RequestParams();
        params.put("page", 1);

        HttpUtil.post(Url.newxuqiu, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d(response.toString(), "333333333333333");
                NewXuqiuModel newXuqiuModel = new NewXuqiuModel();
                Gson gson = new Gson();
                newXuqiuModel = gson.fromJson(response.toString(), NewXuqiuModel.class);

                recycle_xuqiu.setLayoutManager(new FullyLinearLayoutManager(getActivity()));
                NewXuQiuAdapter newXuQiuAdapter = new NewXuQiuAdapter(newXuqiuModel.getDataObject());
                Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.line_divider);
//                recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext(), drawable, 2));
                recycle_xuqiu.setAdapter(newXuQiuAdapter);

            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                ShowDialog.dissmiss();

            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });
    }
//最热需求

    public void gethotXuqiu() {

        final RequestParams params = new RequestParams();
        params.put("page", 1);

        HttpUtil.post(Url.newhotxuqiu, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d(response.toString(), "333333333333333");
                NewHotModel newXuqiuModel = new NewHotModel();
                Gson gson = new Gson();
                newXuqiuModel = gson.fromJson(response.toString(), NewHotModel.class);
//                recycle_xuqiu.setLayoutManager(new Full(getActivity(), 2));
//                recycle_xuqiu.setLayoutManager(new LinearLayoutManager(getActivity()));

                NewHotXuqiuAdapter newXuQiuAdapter = new NewHotXuqiuAdapter(newXuqiuModel.getDataObject());
                Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.line_divider);
                recycle_xuqiu.addItemDecoration(new SimpleDividerItemDecoration(getContext(), drawable, 2));
                recycle_xuqiu.setAdapter(newXuQiuAdapter);


            }

            @Override
            public void onFinish() {
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                DBLog.e("tag", statusCode + ":" + responseString);
                ShowDialog.dissmiss();

            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "请求取消", Toast.LENGTH_SHORT).show();
                ShowDialog.dissmiss();
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}